import javax.servlet.annotation.WebServlet;
import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;

@WebServlet( urlPatterns = {"/check"})
public class CheckAvailability extends HttpServlet {

    private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String URL = getServletContext().getInitParameter("db.url");
        String NAME = getServletContext().getInitParameter("db.user");
        String PASSWORD = getServletContext().getInitParameter("db.password");
	System.out.println("aaaa");
        try (Connection conn = DriverManager.getConnection(URL, NAME, PASSWORD)) {

            String uname = request.getParameter("username");
            PreparedStatement ps = conn.prepareStatement("select username from user where username=?");
            ps.setString(1, uname);
            ResultSet rs = ps.executeQuery();

            if (!rs.next()) {
                out.println("<font color=red><b>" + uname + "</b> 用户名不存在,点击下方创建账号");

            } 
            out.println();

        } catch (Exception ex) {

            out.println("Error ->" + ex.getMessage());

        }
        finally {
            out.close();

        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        doPost(request, response);

    }

}
